<template>
  <div class="dashboard-container">
    <div class="flex1" style="justify-content: space-between;">
      <el-card style="width: 45%" class="ptd-30">
        <div class="flex_l">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p>类型：
          </div>
          <el-select class="w-350 ml-20" v-model="form.type" placeholder="请选择">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div class="flex_l mt-20">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p> 联系人姓名：
          </div>
          <el-input class="w-350 ml-20" v-model="form.collName" placeholder="请输入"></el-input>
        </div>
        <div class="flex_l mt-20">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p> 绑定手机号：
          </div>
          <el-input class="w-350 ml-20" v-model="form.collPhone" placeholder="请输入"></el-input>
        </div>

        <!-- <div class="flex_l mt-20">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p> 绑定手机号：
          </div>
          <el-input class="w-350 ml-20" v-model="form.phone" placeholder="请输入"></el-input>
        </div> -->
        <div class="flex_l mt-20">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p> 开户账号：
          </div>
          <el-input class="w-350 ml-20" v-model="form.yinHangKaHao" placeholder="请输入"></el-input>
        </div>

        <div class="flex_l mt-20">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p> 开户名：
          </div>
          <el-input class="w-350 ml-20" v-model="form.kaiHuMing" placeholder="请输入"></el-input>
        </div>
        <div class="flex_l mt-20">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p> 联行号：
          </div>
          <div class="input" @click="dialogVisible = true"><a href="#" style="color: #333;margin-left: 14px;">{{
            form.lianHangHao || '请选择' }}</a></div>

          <!-- <el-input class="w-350 ml-20" v-model="form.lianHangHao" placeholder="请输入"></el-input>
          <a @click="dialogVisible = true" class="size-16 ml-30" style="color: #333;">查找联行号</a> -->
        </div>
        <div class="flex_l mt-20">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p> <p v-if="form.type == 1">法人(商户)</p>名字：
          </div>
          <el-input class="w-350 ml-20" v-model="form.faRenName" placeholder="请输入"></el-input>
        </div>
        <div class="flex_l mt-20">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p> <p v-if="form.type == 1">法人(商户)</p> 身份证号：
          </div>
          <el-input class="w-350 ml-20" v-model="form.faRenShenFenZhengHao" placeholder="请输入"></el-input>
        </div>
        <div class="flex_l mt-20" v-if="form.type == 1">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p> 组织机构代码：
          </div>
          <el-input class="w-350 ml-20" v-model="form.zhuZhiJiGouHaoMa" placeholder="请输入"></el-input>
        </div>

        <div class="flex_l mt-20">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p><p v-if="form.type == 1">法人(商户)</p>身份证正面：
          </div>
          <div class="ml-20">
            <el-upload class="" :action="upUrl" list-type="picture-card" :on-success="handlePictureCardPreview('faRenShenFenZhengZhengMian')
              " :on-remove="handleRemove('faRenShenFenZhengZhengMian')" :limit="1" :file-list="imgList1">
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </div>
        <div class="flex_l mt-20">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p> <p v-if="form.type == 1">法人(商户)</p>身份证反面：
          </div>
          <div class="ml-20">
            <el-upload class="" :action="upUrl" list-type="picture-card"
              :on-success="handlePictureCardPreview('faRenShenFenZhengFanMian')"
              :on-remove="handleRemove('faRenShenFenZhengFanMian')" :limit="1" :file-list="imgList2">
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </div>
        <div class="flex_l mt-20" v-if="form.type == 1">
          <div class="w-150 flex_r">
            <p class="red pr-3">*</p> 营业执照：
          </div>
          <div class="ml-20">
            <el-upload class="" :action="upUrl" list-type="picture-card"
              :on-success="handlePictureCardPreview('yingYeZhiZhao')" :on-remove="handleRemove('yingYeZhiZhao')"
              :limit="1" :file-list="imgList3">
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </div>
        <div class="flex_l mt-20">
          <div class="w-150 text-right">联系人邮箱：</div>
          <el-input class="w-350 ml-20" v-model="form.collEmail" placeholder="请输入"></el-input>
        </div>
        <div class="flex_l mt-20">
          <div class="w-150 text-right">开户行：</div>
          <el-input class="w-350 ml-20" v-model="form.kaiHuHang" placeholder="请输入"></el-input>
        </div>
        <div class="flex_l mt-20">
          <div class="w-150 text-right">选择城市：</div>
          <el-cascader class="w-350 ml-20" placeholder="省/市/区" :options="addresslist" :props="cityProps" v-model="address"
            popper-class="pc-sel-area-cascader" @change="holdTypechange" />
        </div>
        <div class="flex_l mt-20">
          <div class="w-150 text-right">通讯地址：</div>
          <el-input class="w-350 ml-20" v-model="form.address" placeholder="请输入"></el-input>
        </div>
        <div class="flex mt-30">
          <div></div>
          <div class="center">
            <el-button type="primary" class="mr-50" @click="submit"
              :disabled="$store.state.user.userInfo.userExt.payAccount != 'null' && $store.state.user.userInfo.userExt.payAccount != '' && $store.state.user.userInfo.userExt.payAccount != null">确
              定</el-button>
          </div>
        </div>
      </el-card>
      <div style="width: 40%;">
        <div class="flex w100">
            <div class="text-center">
                <p class="bold size-18">总提现金额</p>
                <p class="size-16">{{ info.zongTiXianJinEAll }}</p>
            </div>
            <div class="text-center">
                <p class="bold size-18">可提现金额</p>
                <p class="size-16">{{ info.keTiXianJinEAll }}</p>
            </div>
            <div class="text-center">
                <p class="bold size-18">总交付课时</p>
                <p class="size-16">{{ info.zongJiaoFuKeShiAll }}</p>
            </div>
            <div class="text-center">
                <p class="bold size-18">未交付课时</p>
                <p class="size-16">{{ info.weiJiaoFuKeShiAll }}</p>
            </div>
        </div>
        <div class="flex">
          <div class="center">
            <el-button type="primary" class="w-110" @click="btn(0)">进件</el-button>
            <el-button type="primary" class="w-110" @click="btn(1)">资质上传</el-button>
          </div>
          <el-button type="primary" class="w-110" @click="tixian">提现</el-button>
        </div>
        <div class="flex plr-35">
        <p>交付中心中控大屏</p>

          <!-- <el-select class="w-200" v-model="value1" placeholder="请选择">
            <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select> -->
          <a href="http://121.199.17.238:8004/" target="_blank">打开</a>
        </div>
        <img src="../../assets/daping/4.png" class="image" alt="">
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisible">
      <span slot="title" class="dialog-footer">
        <h4 class="text-center">联行号</h4>
      </span>
      <div class="flex">

        <div>
          <div class="center">
            <div class="flex_l">
              <div class="w-100">选择城市：</div>
              <el-cascader class="w-200" placeholder="省/市" :options="yhAddressList" :props="cityProps"
                v-model="yinhangAddress" popper-class="pc-sel-area-cascader" @change="yinhangAddressChange" />
            </div>
            <div class=" center ml-30">
              <div class="w-120">银行名称：</div>
              <el-input class="" v-model="yinhangValue" placeholder="请输入"></el-input>
            </div>
          </div>
          <div class=" center mt-20">
              <div class="w-120">支行名称：</div>
              <el-input class="" v-model="zhihangValue" placeholder="请输入"></el-input>
            </div>
        </div>
        <div class="flex ml-80">
          <!-- <el-button class="w-100" @click="chongzhi">重置</el-button> -->
          <el-button class="w-100" type="primary" @click="getLianHangHaolist">搜索</el-button>
        </div>
      </div>
      <div class="scroll">
        <el-table :data="lianhangList" border style="width: 100%" class="mt-30">
          <el-table-column prop="privnce" label="省份">
          </el-table-column>
          <el-table-column prop="city" label="城市" width="180">
          </el-table-column>
          <el-table-column prop="bankName" label="支行名称" width="180">
          </el-table-column>
          <el-table-column prop="num" label="联行号">
          </el-table-column>
          <el-table-column prop="zhiHang" label="地址">
          </el-table-column>
          <el-table-column prop="" label="操作">
            <template slot-scope="scope" >
                <el-button size="mini"  @click="handleEdit(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-20">
        <div></div>
        <el-pagination :current-page="page" background layout="prev, pager, next" :total="total"
          @current-change="sizeChange">
        </el-pagination>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import {jiaoFuZhongXinSet,getInfo} from '@/api/user.js'
import cityData from "@/assets/json/pca-code.json";
import { jiaoFuList, jiaoFuZhongXinAll, fenXiaoLogList,tiXian,getLianHangHaolist } from '@/api/financeAdmin.js'

export default {
  name: 'Dashboard',
  data() {
    return {
      address: ["11", "1101", "110101"],
      addresslist: cityData, // cityData 是你提供的数据
      cityProps: {
        value: "code", // 指定选项的值为addresslist对象的code属性
        label: "name", // 指定选项标签为addresslist对象的name属性
        children: "children", // 指定选项的子选项为addresslist对象的children属性
      },
      options: [
        {
          value: "1",
          label: "企业",
        },
        {
          value: "2",
          label: "个人",
        },
      ],
      value1: '',
      form: {
        type: "",
        faRenName: "",
        faRenShenFenZhengHao: "",
        faRenShenFenZhengZhengMian: "",
        faRenShenFenZhengFanMian: "",
        yingYeZhiZhao: "",
        zhuZhiJiGouHaoMa: "",
        province: "",
        city: "",
        area: "",
        address: "",
        collName: "",
        collPhone: "",
        collEmail: "",
        // phone: "",
        kaiHuHang: "",
        kaiHuMing: "",
        yinHangKaHao: "",
        cityCode:'',
        lianHangHao:''
      },
      upUrl:this.$upLoadUrl,
      imgList1:[],
      imgList2:[],
      imgList3:[],
      info:{},
      privnce: '',
      city: '',
      lianhangList: [],
      total: 0,
      page: 1,
      yinhangAddress: [],
      yinhangValue: '',
      dialogVisible: false,
      yhAddressList:[],
      zhihangValue:''
    }

  },
  computed:{
    ...mapState(['user'])
  },
  mounted(){
    this.init()
    this.fuWuShangAll(),
    this.setyhAddress()
    this.getLianHangHaolist()
  },
  methods: {
    handleEdit(e){
      console.log(e)
      this.form.lianHangHao = e.num
      this.dialogVisible = false
    },
    sizeChange(e) {
      this.page = e
      this.getLianHangHaolist()
    },
    getLianHangHaolist() {
      getLianHangHaolist({
        pageNum: this.page,
        bankName: this.yinhangValue,
        city: this.city,
        privnce: this.privnce,
        zhiHang:this.zhihangValue
      }).then(res => {
        this.lianhangList = res.rows
        this.total = res.pages
      })
    },
    chongzhi() {
      this.yinhangValue = ''
      this.yinhangAddress = []
      this.city = '',
        this.privnce = '',
        this.page = 1
      this.getLianHangHaolist()
    },
    setyhAddress() {
      const result = cityData.map(province => {
        return {
          code: province.code,
          name: province.name,
          children: province.children.map(city => ({
            code: city.code,
            name: city.name,
            // children: [] // 删除区级数据，设置为空数组
          }))
        };
      });
      this.yhAddressList = result
    },

    yinhangAddressChange(e) {
      var result = this.findMatchingData(this.yhAddressList, e);
      this.privnce = result[0].name
      this.city = result[1].name
    },
    tixian() {
        this.$prompt('请输入提现金额', '提现', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            inputPattern: /^(?!0(\.0+)?$)\d+(\.\d+)?$/,
            inputErrorMessage: '请输入正确提现金额'
        }).then(({ value }) => {
            if (value > this.info.keTiXianJinEAll) {
                this.$message({
                    type: 'error',
                    message: '超出可提现金额'
                });
            } else {
                tiXian({
                    amount: value
                }).then(res => {
                    if (res.code == 0) {
                        this.fuWuShangAll()
                    } else {
                        this.$message({
                            type: 'error',
                            message: res.msg || '提现失败'
                        });
                    }
                })
            }
        })
    },
    fuWuShangAll() {
        jiaoFuZhongXinAll().then(res => {
            this.info = res.rows[0]
        })
    },
    btn(index) {
        var arr = [
            'http://121.199.17.238:8010/merchant_entry/entry/toEntry.action',
            'http://121.199.17.238:8010/merchant_entry/entry/toUpload.action',
            'http://121.199.17.238:8010/merchant_entry/entry/toOpenProduct.action',
            'https://panxueyou.oss-cn-hangzhou.aliyuncs.com/static/renZhengMa.png',
            'http://121.199.17.238:8010/merchant_entry/entry/toWxApply.action'
        ]
        window.open(arr[index], '_blank');

    },
    findMatchingData(data, codes, type = "code") {
      let result = [];

      for (let item of data) {
        // 根据传入的 type 参数判断是根据 'name' 还是 'code' 匹配
        if (type === "code" && codes.includes(item.code)) {
          result.push(item); // 如果匹配，添加 id 到结果数组
        } else if (type === "name" && codes.includes(item.name)) {
          result.push(item); // 如果匹配，添加 id 到结果数组
        }

        // 如果当前对象有 children，则递归查找
        if (item.children && item.children.length > 0) {
          result = result.concat(
            this.findMatchingData(item.children, codes, type)
          );
        }
      }

      return result;
    },
    holdTypechange(e) {
      this.form.cityCode = e+''
      var result = this.findMatchingData(this.addresslist, e);
      this.form.province = result[0].name;
      this.form.city = result[1].name;
      this.form.area = result[2].name;
    },
    init(){
      // console.log(this.user.userInfo.userExt.id)
      // return
      getInfo().then(res=>{
        var userInfo = JSON.parse(res.rows[0].userExt.config);
        for (var i in this.form) {
          this.form[i] = userInfo[i];
        }
        this.imgList1 = [
          { name: "1", url: userInfo.faRenShenFenZhengZhengMian },
        ];
        this.imgList2 = [{ name: "1", url: userInfo.faRenShenFenZhengFanMian }];
        this.imgList3 = [{ name: "1", url: userInfo.yingYeZhiZhao }];
        this.address = userInfo.cityCode.split(',')

      })
    },
    submit(){
      if(this.form.type == 2) this.form.zhuZhiJiGouHaoMa = this.form.faRenShenFenZhengHao

      for (var i in this.form) {
        var arr = ['collEmail', 'kaiHuHang', 'address', 'province', 'city', 'area','cityCode','yingYeZhiZhao']

        if (!arr.includes(i)) {
          if (
            this.form[i] === "" ||
            this.form[i] === 0 ||
            this.form[i] === false
          ) {
            return this.$message({
              showClose: true,
              message: "请填写完整信息",
              type: "warning",
            });
          }
        }
      }
      jiaoFuZhongXinSet({
        id: this.$store.state.user.userInfo.userExt.id,
        config: this.form,
      }).then(res=>{
        this.$store.dispatch('user/getInfo')
        this.$message({
            message: '修改成功',
            type: 'success'
        });
      })
    },
    handleRemove(type) {
      return (file, fileList) => {
        this.form[type] = ''
      };
    },
    handlePictureCardPreview(type) {
      console.log(type)
      return (response, file) => {
        this.form[type] = response.list[0].url
      };
    }
  }
}
</script>

<style lang="scss" scoped>
.input{
  border-radius: 4px;
  border: 1px solid #DCDFE6;
  font-size: inherit;
  height: 40px;
  line-height: 40px;
  width: 350px;
  margin-left: 20px;
}
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}

a {
  color: #129;
}

.image {
  object-fit: cover;
  width: 90%;
  height: 350px;
  // margin-top: 20px;
  margin: 20px auto 0;
}
</style>
